package com.example.service.impl;

import com.example.mapper.StudentLogMapper;
import com.example.pojo.StudentDeleteLog;
import com.example.service.StudentLogService;
import com.example.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class StudentLogServiceImpl implements StudentLogService {
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private StudentLogMapper studentLogMapper;
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)  //这里还要开启一个事务，不加入上一级方法开启的事务
    public void studentDeleteLog(Integer id) {
        String jwt = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        Integer operatedId = (Integer) claims.get("id");
        StudentDeleteLog studentDeleteLog = new StudentDeleteLog();
        studentDeleteLog.setOperateId(operatedId);
        studentDeleteLog.setDeleteId(id);
        studentDeleteLog.setCreateTime(LocalDateTime.now());
        studentDeleteLog.setDescription("此次删除的学生主键为"+id);
        studentLogMapper.studentDeleteLog(studentDeleteLog);
    }
}
